package com.wy.bezierdragdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.drawable.AnimationDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

/**
 * Time:2021/9/16
 * Author:ypf
 * Description:
 */
public class BubbleMessageTouchListener implements View.OnTouchListener, MessageBubbleView.MessageBubbleListener {

    //原来需要拖动爆炸的View
    private View mStaticView;

    private WindowManager mWindowManager;
    private MessageBubbleView mMessageBubbleView;
    private WindowManager.LayoutParams mParams;
    private Context mContext;

    //爆炸的动画
    private FrameLayout mBombFrame;
    private ImageView mBombImageView;
    private MessageBubbleView.BubbleDisappearListener disappearListener;

    public BubbleMessageTouchListener(Context context, View staticView) {
        mStaticView = staticView;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mMessageBubbleView = new MessageBubbleView(context);
        mMessageBubbleView.setMessageBubbleListener(this);
        mParams = new WindowManager.LayoutParams();
        mParams.format = PixelFormat.TRANSPARENT;
        mContext = context;

        mBombFrame = new FrameLayout(mContext);
        mBombImageView = new ImageView(mContext);
        mBombImageView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mBombFrame.addView(mBombImageView);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //自己隐藏
                mStaticView.setVisibility(View.INVISIBLE);
                //要在WindowManger上面搞一个View，上一节写好的贝瑟尔的View
                mWindowManager.addView(mMessageBubbleView, mParams);
                //初始化贝瑟尔View的点
                //保证固定圆在view的中心
                int[] local = new int[2];
                mStaticView.getLocationOnScreen(local);
                Bitmap bitmap = getBitmapView(mStaticView);
                mMessageBubbleView.initPoint(local[0] + mStaticView.getWidth() / 2, local[1] + mStaticView.getWidth() / 2 - BubbleUtils.getStatusBarHeight(mContext));
                //给消息拖拽设置一个Bitmap
                mMessageBubbleView.setDragBitmap(bitmap);
                break;
            case MotionEvent.ACTION_MOVE:
                mMessageBubbleView.updateDragPoint(event.getRawX(), event.getRawY() - BubbleUtils.getStatusBarHeight(mContext));
                break;
            case MotionEvent.ACTION_UP:
                mMessageBubbleView.handleActionUp();
                break;
        }
        return true;
    }

    //从一个view中获取Bitmap
    private Bitmap getBitmapView(View view) {
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

    @Override
    public void restore() {
        //把消息的View移除
        mWindowManager.removeView(mMessageBubbleView);
        //把原来的view显示
        mStaticView.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismiss(PointF pointF) {
        //要去执行爆炸动画 帧动画
        //原来的view肯定要移除
        mWindowManager.removeView(mMessageBubbleView);
        //要在WindowManager添加一个爆炸动画
        mWindowManager.addView(mBombFrame, mParams);
        //设置背景
        mBombImageView.setBackgroundResource(R.drawable.anim_bubble_pop);
        AnimationDrawable drawable = (AnimationDrawable) mBombImageView.getBackground();
        //设置位置
        mBombImageView.setX(pointF.x - drawable.getIntrinsicWidth() / 2);
        mBombImageView.setY(pointF.y - drawable.getIntrinsicHeight() / 2);
        //开启动画
        drawable.start();
        //执行完毕后要移除掉mBombFrame
        mBombImageView.postDelayed(new Runnable() {
            @Override
            public void run() {
                //移除
                mWindowManager.removeView(mBombFrame);
                //通知该view消失了
                if (disappearListener != null) {
                    disappearListener.dismiss(mMessageBubbleView);
                }
            }
        }, getAnimationDrawableTime(drawable));
    }

    /**
     * 获取动画执行的时间
     *
     * @param drawable
     * @return
     */
    private long getAnimationDrawableTime(AnimationDrawable drawable) {
        int numberOfFrames = drawable.getNumberOfFrames();
        long time = 0;
        for (int i = 0; i < numberOfFrames; i++) {
            time += drawable.getDuration(i);
        }
        return time;
    }

}
